home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5353 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: lerc.nasa.gov!purdue!yuma!steffend
  2. From: steffend@lamar.colostate.edu (Dave Steffen)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Trouble w/ random numbers...please help
  5. Date: 4 Feb 1996 02:26:15 GMT
  6. Organization: Colorado State University, Fort Collins, CO  80523
  7. Message-ID: <4f15g7$2sl4@yuma.ACNS.ColoState.EDU>
  8. References: <4f0g5u$879@ns.campus.mci.net>
  9. NNTP-Posting-Host: glitch.physics.colostate.edu
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Daniel Cotter (cotter@www.cns.uky.edu) wrote:
  13. > I'm trying to write a program that will generate a set of
  14. > random numbers between 3 and 18. I have several problems with
  15. > this program.
  16. > 1. C++ always generates same number(346)
  17. > 2. I don't know how to give a range. I've tried 3-18, but
  18. >    it thinks I mean to subtract(3-18=15)
  19. > 3. When it does work, it's always the same number
  20.  
  21.     OK, first of all: computers are deterministic machines, so
  22. they can't generate truly random number sequences without special
  23. hardware.
  24.     Typically, "random number generators" are functions that
  25. generate a sequence of numbers, but they need some number to start
  26. from. This number is called a "seed". For a given seed, the "random"
  27. numbers generated will always be the _same_ random numbers - but
  28. they'll be statistically uncorrelated _with each other_.
  29.  
  30.     My guess is that you're not initializing your random number,
  31. so the seed is some default value; thus you'll always get the same
  32. sequence. A common way to initialize the generator is to create a seed
  33. value based on the current date and time, thereby generating a
  34. different seed each time the program is run.
  35.  
  36.     As for the other problems, check your documentation for the
  37. rand() and/or srand() routines. They're usually not that good, but
  38. they'll get you started. If you need better routines, check "Numerical
  39. Recipes in C, 2nd Ed".
  40.  
  41.                                  /\
  42.                                  \/
  43.  
  44. Dave Steffen                      No, his mind is not for rent
  45. Dept. of Physics                  To any God or Government
  46. Colorado State University         Always hopeful, yet discontent
  47. steffend@lamar.colostate edu      He knows changes aren't permanent-
  48.                       But change is...
  49. "Speak softly...                    
  50. ... and carry a black belt!"              -Neal Peart / RUSH
  51. -----------------------------------------------------------------------
  52.